package junit;

import org.junit.*;
import static org.junit.Assert.* ;

import fast.StateManager;

public class StateManagerTest {
	
	private StateManager state;
	
	@Test
	public void check_contains_id(){
		//pass
		for(int i=1; i<4; i++){
			System.out.println("Test: check_contains_id " + i + " of 5");
			System.out.println("******************");
			
			assertTrue(state.contains(i));
			System.out.println("Test passed.\n\n");
		}
		
		//fail
		System.out.println("Test: check_contains_id 5 of 5");
		System.out.println("******************");
		assertFalse(state.contains(4));
		System.out.println("Test passed.\n\n");
	}
	
	@Test
	public void check_contains_ip(){
		//pass
		for(int i=1; i<4; i++){
			System.out.println("Test: check_contains_ip " + i + " of 5");
			System.out.println("******************");
			
			assertTrue(state.contains("10."+String.valueOf(i)+".0.1"));
			assertTrue(state.contains("10."+String.valueOf(i)+".0.2"));
			assertTrue(state.contains("10."+String.valueOf(i)+".0.3"));
			assertTrue(state.contains("10."+String.valueOf(i)+".0.4"));
			
			System.out.println("Test passed.\n\n");
		}
		
		//fail
		System.out.println("Test: check_contains_ip 5 of 5");
		System.out.println("******************");
		assertFalse(state.contains("127.0.0.1"));	
		System.out.println("Test passed.\n\n");
	}
	
	@Test
	public void check_contains_ip_id(){
		//pass
		for(int i=1; i<4; i++){
			System.out.println("Test: check_contains_ip_id " + i + " of 5");
			System.out.println("******************");
			
			assertTrue(state.contains(i, "10."+String.valueOf(i)+".0.1"));
			assertTrue(state.contains(i, "10."+String.valueOf(i)+".0.2"));
			assertTrue(state.contains(i, "10."+String.valueOf(i)+".0.3"));
			assertTrue(state.contains(i, "10."+String.valueOf(i)+".0.4"));
			System.out.println("Test passed.\n\n");
		}
		
		//fail
		System.out.println("Test: check_contains_ip_id 5 of 5");
		System.out.println("******************");
		assertFalse(state.contains(5, "127.0.0.1"));
		System.out.println("Test passed.\n\n");
		
	}
	
	@Test
	public void check_remove_id(){
		state.remove(3);
		
		//check entry has been removed
		System.out.println("Test: check_remove_id 1 of 4");
		System.out.println("******************");
		assertFalse(state.contains(3));
		System.out.println("Test passed.\n\n");
		
		//check that other entries still exist
		for(int i = 1; i<3; i++){
			System.out.println("Test: check_remove_id " + i+1 + "of 4");
			System.out.println("******************");
			assertTrue(state.contains(i));	
			System.out.println("Test passed.\n\n");
		}
	}
	
	@Test
	public void check_remove_ip(){	
		state.remove("10.3.0.1");
		
		//check entry has been removed
		System.out.println("Test: check_remove_ip 1 of 4");
		System.out.println("******************");
		assertFalse(state.contains("10.3.0.1"));
		System.out.println("Test passed.\n\n");
		
		//check other entries still exist
		System.out.println("Test: check_remove_ip 2 of 4");
		System.out.println("******************");
		assertTrue(state.contains("10.1.0.1"));
		System.out.println("Test passed.\n\n");
		System.out.println("Test: check_remove_ip 3 of 4");
		System.out.println("******************");
		assertTrue(state.contains("10.2.0.2"));
		System.out.println("Test passed.\n\n");
		System.out.println("Test: check_remove_ip 4 of 4");
		System.out.println("******************");
		assertTrue(state.contains("10.3.0.4"));
		System.out.println("Test passed.\n\n");
	}
	
	@Test
	public void check_remove_ip_id(){
		state.remove(1, "10.1.0.1");
		
		//check entry has been removed
		System.out.println("Test: check_remove_ip_id 1 of 4");
		System.out.println("******************");
		assertFalse(state.contains(1, "10.1.0.1"));
		System.out.println("Test passed.\n\n");
		
		//check other entries still exist
		System.out.println("Test: check_remove_ip_id 2 of 4");
		System.out.println("******************");
		assertTrue(state.contains(1, "10.1.0.2"));
		System.out.println("Test passed.\n\n");
		System.out.println("Test: check_remove_ip_id 3 of 4");
		System.out.println("******************");
		assertTrue(state.contains(2, "10.2.0.2"));
		System.out.println("Test passed.\n\n");
		System.out.println("Test: check_remove_ip_id 4 of 4");
		System.out.println("******************");
		assertTrue(state.contains(3, "10.3.0.3"));
		System.out.println("Test passed.\n\n");
	}
	
	
	
	@Before public void load_state(){
		state = new StateManager();
		
		state.add(1, "10.1.0.1");
		state.add(1, "10.1.0.2");
		state.add(1, "10.1.0.3");
		state.add(1, "10.1.0.4");
		state.add(2, "10.2.0.1");
		state.add(2, "10.2.0.2");
		state.add(2, "10.2.0.3");
		state.add(2, "10.2.0.4");
		state.add(3, "10.3.0.1");
		state.add(3, "10.3.0.2");
		state.add(3, "10.3.0.3");
		state.add(3, "10.3.0.4");
		
	}

}
